# The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
#
# P   A   H   N
# A P L S I I G
# Y   I   R
# And then read line by line: "PAHNAPLSIIGYIR"

# Example 1:
#
# Input: s = "PAYPALISHIRING", numRows = 3
# Output: "PAHNAPLSIIGYIR"
# Example 2:
#
# Input: s = "PAYPALISHIRING", numRows = 4
# Output: "PINALSIGYAHRPI"
# Explanation:
#
# P     I    N
# A   L S  I G
# Y A   H R
# P     I
# 即使得字符串根据锯齿状排列。按照列的角度来观察。

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if s == "" or numRows < 1:
            return
        if numRows == 1:
            return s
        sr = ''
        l = len(s)
        k = 0
        spera = numRows - 2
        for i in range(numRows):
            sn = spera - i
            sr += s[k]
            k += numRows - i + sn
            while k < l:

                for j in range(sn):
                    sr += ' '

                sr += s[k]

                for h in range(i - sn - 1):
                    sr += ' '

                if i != 0 and i != numRows - 1 and k + 2 * i < l:
                    sr += s[k + 2 * i]
                    k += 2 * i
                k += numRows - i + sn

            print(sr)
            sr = ''
            k = i + 1


solution = Solution()
solution.convert("PAYPALISHIRING", 4)
